<template>
  <table>
      <tr>
          <td>
              <div style="width:600px">
                  <vxe-table :data.sync="instrdata" @cell-click="selectInstr" border fit height="200px" header-align="center" highlight-current-row highlight-hover-row highlight-hover-column resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" ref="xtabletop" width="auto" stripe>
                      <vxe-table-column field="instrname" title="仪器名称" width="160px" ></vxe-table-column>
                      <vxe-table-column field="instrno" title="仪器代码" width="100px" ></vxe-table-column>
                      <vxe-table-column field="instrid" title="仪器ID" width="100px" ></vxe-table-column>
                      <vxe-table-column field="instrclass" title="仪器大类" width="100px" ></vxe-table-column>
                  </vxe-table>
              </div>
          </td>
          <td style="text-align:center">
              <div>
                  <table style="width:100%">
                      <tr>
                          <td style="text-align:center">
                              <el-button @click="confirm()">确 定</el-button>
                          </td>
                      </tr>
                      <tr>
                          <td>&nbsp;</td>
                      </tr>
                      <tr>
                          <td style="text-align:center">
                              <el-button @click="cancel()">取 消</el-button>
                          </td>
                      </tr>
                  </table>
              </div>
          </td>
      </tr>
      <tr>
          <td colspan="2">
              <div style="width:760px">
                  <vxe-table :data.sync="qcsdata" @cell-dblclick="selectQcs" border fit height="200px" header-align="center" highlight-current-row highlight-hover-row highlight-hover-column resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" ref="xtablebottom" width="auto" stripe>
                      <vxe-table-column field="qcid" title="ID" width="60px" sortable></vxe-table-column>
                      <vxe-table-column field="qcname" title="质控品名称" width="160px" sortable></vxe-table-column>
                      <vxe-table-column field="batchno" title="批号" width="100px" sortable></vxe-table-column>
                      <vxe-table-column field="qclevel" title="水平" width="100px" sortable></vxe-table-column>
                      <vxe-table-column field="manufacturercode" title="生产厂家" width="160px" sortable></vxe-table-column>
                      <vxe-table-column field="vendoncode" title="供应商" width="160px" sortable></vxe-table-column>
                      <vxe-table-column field="startdt" title="启用日期" width="160px" sortable></vxe-table-column>
                      <vxe-table-column field="expriedt" title="有效期" width="160px" sortable></vxe-table-column>
                      <vxe-table-column field="stopdt" title="停用日期" width="160px" sortable></vxe-table-column>
                      <vxe-table-column field="checkreportflag" title="报告依据" width="160px" sortable></vxe-table-column>
                  </vxe-table>
              </div>
          </td>
          
      </tr>
  </table>
</template>
<script>
export default {
  props: {
      rptunitid: { type: Number },
      reportid: { type: String },
  },
  data() {
      return {
          instrdata:[],
          qcsdata:[],
      };
  },
  created() {
      this.QueryInfo();
  },
  methods: {
      QueryInfo() {
          this.instrdata=[];
          this.qcsdata=[];
          if(this.$refs.xtabletop)  this.$refs.xtabletop.clearCurrentRow();
          if(this.$refs.xtablebottom)   this.$refs.xtablebottom.clearCurrentRow();
          let that = this;
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS310",
                  method: "post",
                  data: {
                      action: "getinstrqcsdata",
                      rptunitid: this.rptunitid,
                      reportid: this.reportid,
                      datatype:1,
                  },
              })
              .then(res => {
                  if (res) {
                      that.instrdata=res.data;
                      if (that.instrdata && that.instrdata.length > 0) {
                          that.$nextTick(() => {
                              if(that.$refs.xtabletop.afterFullData && that.$refs.xtabletop.afterFullData.length >0 )
                              {
                                  let row = that.$refs.xtabletop.afterFullData[0];
                                  if(row){
                                      that.$refs.xtabletop.setCurrentRow(row);
                                      that.selectInstr({row:row}) ;
                                  }
                              }
                          });
                      }
                  }
              });
      },
      selectInstr({ row, rowIndex }) {
          this.qcsdata=[];
          if(this.$refs.xtablebottom)   this.$refs.xtablebottom.clearCurrentRow();
          let that = this;
          console.log('instrid',row);
          this.$tool
              .request({
                  url: "LIS/LIS3/LIS310",
                  method: "post",
                  data: {
                      action: "getinstrqcsdata",
                      rptunitid: this.rptunitid,
                      reportid: this.reportid,
                      instrid: row.instrid,
                      datatype: 2,
                  },
              })
              .then(res => {
                  if (res) {
                      that.qcsdata=res.data;
                      if (that.qcsdata && that.qcsdata.length > 0) {
                          that.$nextTick(() => {
                              if(that.$refs.xtablebottom.afterFullData && that.$refs.xtablebottom.afterFullData.length >0 )
                              {
                                  let row = that.$refs.xtablebottom.afterFullData[0];
                                  if(row){
                                      that.$refs.xtablebottom.setCurrentRow(row);
                                  }
                              }
                          });
                      }
                  }
              });
      },
      cancel() {
          this.$emit("close",'');
      },
      selectQcs({ row, rowIndex }){
          this.confirmqc(row);
      },
      confirm() {
         let row = this.$refs.xtablebottom.getCurrentRow();
         console.log('row',row);
         if(row){
             this.confirmqc(row);
         }
         else{
         }
      },
      confirmqc(row) {
         this.$tool
              .request({
                  url: "LIS/LIS3/LIS310",
                  method: "post",
                  data: {
                      action: "toqcdata",
                      rptunitid: this.rptunitid,
                      reportid: this.reportid,
                      instrid: row.instrid,
                      qcid: row.qcid,
                  },
              })
              .then(res => {
                   if (res.flag==1) {
                       this.$emit("close", res.msg);
                  }
                  else if (res.flag==-1) {
                      this.$alert(res.msg);
                  }
                  else if (res.flag==2) {
                      this.$confirm(res.msg, "提示", {
                          focusCancelButton: true,
                          closeOnClickModal: false,
                          type: "warning",
                      })
                      .then(() => {
                          this.$tool
                              .request({
                                  url: "LIS/LIS3/LIS310",
                                  method: "post",
                                  data: {
                                      action: "toqcdata",
                                      rptunitid: this.rptunitid,
                                      reportid: this.reportid,
                                      instrid: row.instrid,
                                      qcid: row.qcid,
                                      execflag:1,
                                  },
                              }) .then((res1) => {
                                  if (res1.flag==1) {
                                      this.$emit("close", res1.msg);
                                  }
                                  else if (res1.flag==-1) {
                                      this.$alert(res1.msg);
                                  }
                              })
                              .catch((action) => {
                              });
                      })
                      .catch((action) => {
                      });
                  }
              });
      },
  }
};
</script>